///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef __BUOLA_CONTAINER_CBUFFERCONSUMER_H__
#define __BUOLA_CONTAINER_CBUFFERCONSUMER_H__

#include <buola/container/cbuffer.h>
#include <buola/container/cfixedbuffer.h>

namespace buola {

template<typename tType>
class CBufferConsumer
{
public:
    CBufferConsumer(const CBuffer<tType> &pBuffer)
        :   mData(pBuffer.Data())
        ,   mSize(pBuffer.Size())
    {}

    template<std::size_t pCapacity>
    CBufferConsumer(CFixedBuffer<tType,pCapacity> &pBuffer)
        :   mData(pBuffer.Data())
        ,   mSize(pBuffer.Size())
    {}

    bool Empty()
    {
        return mSize==0;
    }

    std::size_t Available()   {   return mSize;   }

    const tType *Data()
    {
        return mData;
    }

    const tType *ConsumeData(std::size_t pSize)
    {
        assert(pSize<=mSize);
        const tType *lOldPtr=mData;
        mData+=pSize;
        mSize-=pSize;
        return lOldPtr;
    }

    std::size_t ConsumeDataUntil(const tType *pPtr)
    {
        std::size_t lConsumed=pPtr-mData;
        assert(lConsumed<=mSize);
        mSize-=lConsumed;
        mData+=lConsumed;
        return lConsumed;
    }

private:
    const tType *mData;
    std::size_t mSize;
};

template<typename tType,std::size_t pCapacity>
inline std::size_t push_data(CBufferConsumer<tType> &pSrc,CFixedBuffer<tType,pCapacity> &pDst,std::size_t pMax=pCapacity)
{
    std::size_t lToCopy=std::min(std::min(pMax,pSrc.Available()),pDst.FreeCapacity());

    pDst.PushData(pSrc.ConsumeData(lToCopy),lToCopy);

    return lToCopy;
}

template<typename tType,std::size_t pCapacity>
inline std::size_t adjust_buffer(CFixedBuffer<tType,pCapacity> &pDst,CBufferConsumer<tType> &pCons)
{
    std::size_t lConsumed=pCons.Data()-pDst.Data();

    pDst.ConsumeData(lConsumed);

    return lConsumed;
}

/*namespace buola*/ }

#endif
